/*pmFWCFu5nhKkBzYmrkBakV0bJk4JW5gQtRRkgMJz728LT5N2JTIvOitt7vMR49l2*/
import { createPage, ajax, base, cardCache } from 'nc-lightapp-front';

import constants from '../../constants';

import { addTransitypeBtns } from '../../utils';

import ampub from 'ampub';
import fa from 'fa';

import { setBatchBtnsEnable } from './rowSelected';
import tableButtonClick, { linkToCardOnRow } from './tableButtonClick';
import { setSearchDefaultValue } from './searchBtnClick';

const {
	components: { AMTooltip, LoginContext: { loginContext, getContext, loginContextKeys } },
	utils: { multiLangUtils: { getMultiLangByID } }
} = ampub;

const { fa_components: { ReferFilter: { addSearchAreaReferFilter } } } = fa;

const {
	UISTATE,
	LIST_BTNS: { TABLE_BODY },
	ReferFilterConfig,
	BTN_AREA,
	BILL_SOURCE_TYPES,
	assetDataSource,
	AREA: { LIST: { GRID_AREA: tableId, SEARCH_AREA: searchId } }
} = constants;

export default function(props) {
	const that = this;
	props.createUIDom(
		{
			pagecode: constants.PAGE_CODE.LIST //页面id
		},
		function(data) {
			if (data) {
				if (data.context) {
					// 初始化上下文变量
					loginContext(data.context);
				}
				if (data.button) {
					//小应用中注册的按钮
					let button = data.button;
					//获取当前语种
					let currentLangSeq = getContext(loginContextKeys.languageIndex);
					addTransitypeBtns.call(that, button, currentLangSeq);
					props.button.setButtons(button, () => {
						setBatchBtnsEnable(props, tableId);
					});
				}
				if (data.template) {
					//返回模版信息
					that.templateid = data.template.pageid;

					let meta = data.template;
					meta = modifierMeta.call(that, props, meta);
					props.meta.setMeta(meta, () => {
						doInit(that);
					});
				}

				// 行删除时悬浮框提示
				/* 国际化处理： 确定要删除吗？*/
				props.button.setPopContent('Delete', getMultiLangByID('msgUtils-000001'));
			}
		}
	);
}

function modifierMeta(props, meta) {
	const that = this;

	//添加复制卡片对话框模板
	that.facardCopy.initCopyCardTemplate(meta);
	//分页控件显示标识
	meta[tableId].pagination = true;

	//添加参照过滤
	addSearchAreaReferFilter(props, meta, ReferFilterConfig);

	meta[tableId].items = meta[tableId].items.map((item, key) => {
		//单据号添加下划线超链接
		if (item.attrcode == 'asset_code') {
			item.render = (text, record, index) => {
				return (
					<div className="simple-table-td">
						<AMTooltip>
							<span
								className="code-detail-link"
								onClick={() => {
									linkToCardOnRow(props, record, UISTATE.BROWSE);
								}}
							>
								{record.asset_code && record.asset_code.value}
							</span>
						</AMTooltip>
					</div>
				);
			};
		}
		return item;
	});
	//添加操作列
	meta[tableId].items.push({
		attrcode: 'opr',
		label: getMultiLangByID('amcommon-000000') /* 国际化处理： 操作*/,
		width: 200,
		itemtype: 'customer', //操作列的制定类型
		fixed: 'right',
		className: 'table-opr',
		visible: true,
		render: (text, record, index) => {
			return (
				<div>
					{//根据 saga 状态创建按钮
					props.button.createErrorButton({
						record: record,
						sucessCallBack: () => {
							//saga 状态为 success 时，显示业务按钮
							let { EDIT, COPY, DELETE, QueryAboutBusiness } = TABLE_BODY;
							let buttonAry = [ EDIT, COPY, DELETE ];

							let {
								// 来源单据类型
								bill_source: { value: bill_source },
								// 来源单据交易类型
								transi_type_source: { value: transi_type_source },
								// 来源单据pk
								pk_bill_src: { value: pk_src }
							} = record;

							//获取卡片来源交易类型 有默认值handin
							if (transi_type_source === BILL_SOURCE_TYPES.handin_src) {
								transi_type_source = null;
							}
							// 获取卡片来源单据类型
							let type_src = transi_type_source || bill_source;

							if (type_src && pk_src && !type_src.startsWith(BILL_SOURCE_TYPES.equip_src)) {
								buttonAry.push(QueryAboutBusiness); //  联查 单据追溯
							}

							return props.button.createOprationButton(buttonAry, {
								ignoreHotkeyCode: [ COPY, DELETE, QueryAboutBusiness ],
								area: BTN_AREA.LIST_TABLE_INNER,
								buttonLimit: 3,
								onButtonClick: (props, key) => tableButtonClick.call(that, props, key, text, record, index)
							});
						}
					})}
				</div>
			);
		}
	});

	meta[searchId].items.map((item) => {
		if (item.attrcode === 'pk_transitype') {
			item.queryCondition = () => {
				return { parentbilltype: 'H1' };
			};
		}
		//增加方式 允许选择非末级
		if (item.attrcode === 'pk_addreducestyle') {
			item.onlyLeafCanSelect = false;
		}
	});
	return meta;
}

//初始化
function doInit(that) {
	setBatchBtnsEnable(that.props, tableId);
	setSearchDefaultValue.call(that, that.props, searchId);

	//初始化缓存
	let pk2ClientKeyMap = cardCache.getDefData(constants.pk2ClientKeyMap, assetDataSource);
	if (!pk2ClientKeyMap) {
		cardCache.setDefData(constants.pk2ClientKeyMap, assetDataSource, {});
	}
}

/*pmFWCFu5nhKkBzYmrkBakV0bJk4JW5gQtRRkgMJz728LT5N2JTIvOitt7vMR49l2*/